﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraCharts;
using ESOL.Business.Prod;
using ESOL.Common.Entity;
using ESOL.Business.ProcessingService;
using System.Globalization;

namespace ESOL.WinUI.Manufacture
{
    public partial class frmManageCapacityList : BaseForm.frmBaseMdiBusinessForm
    {
        #region vars

        #endregion

        #region ctors
        public frmManageCapacityList()
        {
            InitializeComponent();
        }
        #endregion

        #region functions
        private void BindChart()
        {
            DateTime tStart=new DateTime();
            DateTime tEnd=new DateTime();
            if (dtpStart.EditValue != null)
                tStart = (DateTime)dtpStart.EditValue;
            if (dtpEnd.EditValue != null)
                tEnd = (DateTime)dtpEnd.EditValue;
            DataTable dtCapacity = ProdCapacityService.GetCapacityForTime(tStart, tEnd);
            int nStart = Common.CommonFunctions.WeekOfYear(tStart, new CultureInfo("zh-CN"));
            int nEnd = Common.CommonFunctions.WeekOfYear(tEnd, new CultureInfo("zh-CN"));
            int nPeriod=nEnd-nStart+1;
            DataTable dtTemp = new DataTable();
            dtTemp.Columns.Add("WorkcenterName");
            dtTemp.Columns.Add("TotalCapacity",typeof(decimal));
            dtTemp.Columns.Add("CurrentCapacity", typeof(decimal));
            List<ProdWorkcentersEntity> list = ProdWorkcentersBs.GetAllForCondition("and ACTIVE=1 ", "", "");
            foreach(ProdWorkcentersEntity m in list)
            {
                DataRow dr = dtTemp.NewRow();
                dr["WorkcenterName"] = m.Name;
                dr["TotalCapacity"] = m.WorkingPeriod * nPeriod;
                DataRow[] aryDr=dtCapacity.Select("CODE='"+m.Code+"'");
                if (aryDr.Length != 0)
                {
                    dr["CurrentCapacity"] = aryDr[0]["CAPACITY_TIME"];
                }
                else dr["CurrentCapacity"] = 0;
                dtTemp.Rows.Add(dr);
            }

            chartControl1.Dock = DockStyle.Left;
            int nGridWidth = devSplitContainer1.Panel1.Width;
            if (list.Count * 125 > nGridWidth)
            {
                chartControl1.Width = list.Count * 125;
            }
            else
            {
                chartControl1.Width = nGridWidth;
            }

            this.chartControl1.Series[0].DataSource = dtTemp;// myDataTable是获取到的数据
            this.chartControl1.Series[0].ArgumentDataMember = "WorkcenterName";//绑定的文字信息（名称）
            this.chartControl1.Series[0].ValueDataMembers[0] = "TotalCapacity";//绑定的值（数据）

            this.chartControl1.Series[1].DataSource = dtTemp;// myDataTable是获取到的数据
            this.chartControl1.Series[1].ArgumentDataMember = "WorkcenterName";//绑定的文字信息（名称）
            this.chartControl1.Series[1].ValueDataMembers[0] = "CurrentCapacity";//绑定的值（数据）
        }
        #endregion

        #region events
        protected override void OnShown(EventArgs e)
        {
            dtpStart.EditValue = DateTime.Today;
            dtpEnd.EditValue = DateTime.Today.AddDays(7);
        }
        protected override void Click_Query()
        {
            base.Click_Query();
            BindChart();
        }
        #endregion
    }
}
